package com.pwser.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pwser.dao.OrderDao;
import com.pwser.entity.Order;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class OrderService {
	
	@Autowired
	private OrderDao orderDao;
	
	public void findAllOrders() {
		List<Order> orderlist = orderDao.findAll();
		System.out.println("JPA查询结果-------共"+orderlist.size()+"条数据，第一条数据：" + orderlist.get(0).getOrderName());
	}
	
	public void addOrder() {
		List<Order> orderlist = new ArrayList<Order>();
		
		Order order1 = new Order();
		order1.setOrderId(1001);
        order1.setOrderStatus(0);
        order1.setOrderName("小米6");
        order1.setOrderDate(new Date());
        orderlist.add(order1);
        
        Order order2 = new Order();
        order2.setOrderId(1002);
        order2.setOrderStatus(1);
        order2.setOrderName("小米8");
        order2.setOrderDate(new Date());
        orderlist.add(order2);
        
		orderDao.save(orderlist);
	}
	
	@RabbitListener(queues = "queue.jnpd.exception")
	public void orderExceptionQueue(Message message) {
		String consumerQueue = message.getMessageProperties().getConsumerQueue();
		log.error("###########################################");
		log.error("【"+consumerQueue+"的异常消息】 - 【消息异常时间】 - ["+new Date()+"]- 【异常消息内容】 - ["+message+"]");
		log.error("###########################################");
	}

}
